<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/25
 * Time: 15:35
 */

namespace app\system\controller;




use app\admin\model\ThemeModel;
use app\portal\model\PortalCategoryModel;
use app\system\model\SchoolModel;
use cmf\controller\AdminBaseController;
use think\Db;

class SchoolDistrictController extends AdminBaseController
{
    public  function  index()
    {
        $schoolCategoryTableTree = new SchoolModel();
        $categoryTree        = $schoolCategoryTableTree->schoolCategoryTableTree();
        $this->assign('category_tree', $categoryTree);
        return $this->fetch();
    }
    public function  add()
    {
        $parentId            = $this->request->param('parent', 0, 'intval');
        $SchoolModel = new SchoolModel();
        $categoriesTree      = $SchoolModel->schoolCategoryTree($parentId);

//        $themeModel        = new ThemeModel();
//        $listThemeFiles    = $themeModel->getActionThemeFiles('portal/List/index');
//        $articleThemeFiles = $themeModel->getActionThemeFiles('portal/Article/index');
////
//        $this->assign('list_theme_files', $listThemeFiles);
////        $this->assign('article_theme_files', $articleThemeFiles);
        $this->assign('categories_tree', $categoriesTree);
        return $this->fetch();
    }
    /**
     * 添加校区分类提交
     * @(
     *     'name'   => '校区名称',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $SchoolModel = new SchoolModel();
        if($this->request->isPost()){
            $data = $this->request->post();
        }
        $result = $this->validate($data, 'SchoolCategory');
        if ($result !== true) {
            $this->error($result);
        }
        $result =$SchoolModel->addCategory($data);
        if ($result === false) {
            $this->error('添加失败!');
        }
        $this->success('添加成功!', url('SchoolDistrict/index'));

    }
    //排序的类别
    public function listOrder()
    {
        parent::listOrders(Db::name('school'));
        $this->success("排序更新成功！", '');
    }
 //编辑edit
    public function  edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $category = SchoolModel::get($id)->toArray();
            $this->assign($category);

            $SchoolModel = new SchoolModel();
            $categoriesTree      = $SchoolModel->schoolCategoryTree($category['parent_id'], $id);
            $this->assign('categories_tree', $categoriesTree);
            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }
    }
    /**
     * 编辑文章分类提交
     * @adminMenu(
     *     'name'   => '编辑校区分类提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章分类提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $SchoolModel = new SchoolModel();
        if($this->request->isPost()){
            $data = $this->request->post();
        }
        $result = $this->validate($data, 'SchoolCategory');
        if ($result !== true) {
            $this->error($result);
        }

        $SchoolModel = new SchoolModel();
        $result = $SchoolModel->editCategory($data);
        if ($result === false) {
            $this->error('保存失败!');
        }
        $this->success('保存成功!',url('schoolDistrict/index'));
}
    //删除delete
    public function  delete()
    {
        $schoolCategory=new SchoolModel();
        if (!isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            $res = $schoolCategory->where('id', $id)->find();
            //获取删除的内容
            $data = [
                'object_id' => $res['id'],
                'create_time' => time(),
                'table_name' => 'school_category',
                'name' => $res['name']
            ];
            $resultcategory = $schoolCategory->where('parent_id', $id)->select();
            foreach($resultcategory as $k=>$v)
            {
                if($v['delete_time']==0)
                {
                    $this->error("删除失败,有子类不能删除！",url("schoolDistrict/index"));
                }
            }
//            if(!empty($resultcategory))
//            {
//                $this->error("删除失败,有子类不能删除！",url("schoolDistrict/index"));
//            }
            //伪删除
            $result = $schoolCategory
                ->where('id', $id)
                ->update(['delete_time' => time()]);
            //真的删除
//            $result = $schoolCategory
//                ->where('id', $id)
//                ->delete();
            if ($result) {
                Db::name('recycleBin')->insert($data);
            }else{
                $this->success('删除失败','');
            }

            $this->success("删除成功！", '');
        }
    }
}